package com.penngo.gui.dfdiff.utils;

import net.ifok.image.image4j.codec.bmp.BMPEncoder;
import net.ifok.image.image4j.codec.ico.ICODecoder;
import net.ifok.image.image4j.codec.ico.ICOEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class Image4j {
    private static File tempFile = null;

    public static boolean convertIamgeToICO(String strInputFilePath, String strOutputFilePath){
        String strInFile = strInputFilePath;
        String strOutFile = strOutputFilePath;

        //InputStream in = null;
        try(InputStream in = new FileInputStream(strInFile)) {
            java.util.List<BufferedImage> images;
            //in = new FileInputStream(strInFile);

            if (!(strInFile.toLowerCase().endsWith(".ico"))) {
                images = new ArrayList<BufferedImage>(1);
                images.add(ImageIO.read(in));

                File outFile = new File(strOutFile);
                ICOEncoder.write(images, outFile);
                return true;
            }
            else{
                throw new IOException("strInputFilePath is not expectation endsWith .ico/.ICO");
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public static boolean convertICOToImage(String strInputFilePath, String strOutputFilePath){
        // BMP, JPG, PNG
        String strInFile = strInputFilePath;
        String strOutFile = strOutputFilePath;

        //InputStream in = null;
        try (InputStream in = new FileInputStream(strInFile)){
            java.util.List<BufferedImage> images;
            //in = new FileInputStream(strInFile);

            if (strInFile.toLowerCase().endsWith(".ico")) {
                images = ICODecoder.read(in);

                if(strOutFile.toLowerCase().endsWith(".jpg")){
                    BufferedImage img = images.get(0);
                    File pngFile = new File(strOutFile);
                    ImageIO.write(img, "jpeg", pngFile);

                }
                else if(strOutFile.toLowerCase().endsWith(".png")){
                    // write PNG
                    BufferedImage img = images.get(0);
                    File pngFile = new File(strOutFile);
                    ImageIO.write(img, "png", pngFile);

                }
                else if(strOutFile.toLowerCase().endsWith(".bmp")){
                    // write BMP
                    BufferedImage img = images.get(0);
                    File bmpFile = new File(strOutFile);
                    BMPEncoder.write(img, bmpFile);
                }
                else{
                    throw new IOException("con't convert this fileType");
                }
                return true;
            }
            else {
                throw new IOException("strInputFilePath must be endsWith .ico/.ICO");
            }
        }
        catch(IOException e){
            e.printStackTrace();
            return false;
        }
    }

    public static boolean convertImageToImage(String strInputFilePath, String strOutputFilePath){
        String strInFile = strInputFilePath;
        String strOutFile = strOutputFilePath;

        InputStream in = null;
        try {
            java.util.List<BufferedImage> images;
            in = new FileInputStream(strInFile);
            if(isImage(strInFile))
            {
                images = new ArrayList<>(1);
                images.add(ImageIO.read(in));

                // create temp ico file
                // png/jpg/tmp -> ico -> png/jpg/tmp
                ICOEncoder.write(images, tempFile);
                convertICOToImage(tempFile.toString(), strOutFile);
                return true;
            }
            else{
                throw new IOException("strInputFilePath must be endsWith png/jpg/tmp");
            }
        }
        catch (IOException e){
            e.printStackTrace();
            return false;
        }
        finally {
            try {
                in.close();
            }
            catch (IOException e){}
        }
    }

    private static boolean isImage(String file){
        if((file.endsWith(".jpg") || file.endsWith(".JPG")) ||
                (file.endsWith(".png") || file.endsWith(".PNG")) ||
                (file.endsWith(".bmp") || file.endsWith(".bmp"))){
            return true;
        }
        else{
            return false;
        }
    }

    private static boolean imageConvert(String strInputFilePath, String strOutputFilePath,String formatName){
        BufferedImage bufferedImage;
        try {
            bufferedImage = ImageIO.read(new File(strInputFilePath));
            BufferedImage newBufferedImage = new BufferedImage(
                    bufferedImage.getWidth(), bufferedImage.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0,
                    Color.WHITE, null);
            ImageIO.write(newBufferedImage, formatName, new File(strOutputFilePath));
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        File old = new File("src\\main\\resources\\images\\logo.png");
        File newFile = new File("src\\main\\resources\\images\\logo.ico");




//        File old = new File("DFDiff\\src\\main\\resources\\images\\logo2.png");
//        File oldS = new File("DFDiff\\src\\main\\resources\\images\\logo2_s.png");
//        File newFile = new File("DFDiff\\src\\main\\resources\\images\\logo2.ico");
//        File newFileS = new File("DFDiff\\src\\main\\resources\\images\\logo2_s.ico");
//        Convert(old.getAbsolutePath(),newFile.getAbsolutePath());
//        cn.hutool.core.img.ImgUtil.cut(
//                FileUtil.file(newFile),
//                FileUtil.file(newFileS),
//                new Rectangle(0, 0, 32, 32)//裁剪的矩形区域
//        );
//        cn.hutool.core.img.ImgUtil.scale(
//                FileUtil.file(newFile),
//                FileUtil.file(newFileS),
//                0.5f//缩放比例
//        );
//        cn.hutool.core.img.ImgUtil.scale(
//                FileUtil.file(old),
//                FileUtil.file(oldS),
//                0.5f//缩放比例
//        );
        //jpg -> ico
//        Convert("D:\\Temp\\timg.jpg","D:\\Temp\\timg.ico");

        //jpg -> png
//        Convert("D:\\Temp\\timg.jpg","D:\\Temp\\timg_jpg.png");

        //jpg -> bmp
//        Convert("D:\\Temp\\timg.jpg","D:\\Temp\\timg_jpg.bmp");

        //png -> jpg -> ico
//        Convert("D:\\Temp\\timg.png","D:\\Temp\\timg_png.ico");


        //png -> jpg
//        Convert("D:\\Temp\\timg.png","D:\\Temp\\timg_png.jpg");

        //png -> bmp
//        Convert("D:\\Temp\\timg.png","D:\\Temp\\timg_png.bmp");


        //ico -> png
//        Convert("D:\\Temp\\timg.ico","D:\\Temp\\timg_ico.png");

        //ico -> png -> jpg
//        Convert("D:\\Temp\\timg.ico","D:\\Temp\\timg_ico.jpg");

        //ico -> bmp
//        Convert("D:\\Temp\\timg.ico","D:\\Temp\\timg_ico.bmp");

    }
}
